﻿<?php
// DB
define('DB_DRIVER', 'mysql');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '@huydang1920@');
define('DB_DATABASE', 'book');
define('DB_PREFIX', '');
include('mysql.php');
include('simple_html_dom.php');
include('seo_url.php');
include('utf8.php');
define('URL', 'http://tiki.vn/');
$db = new MySQL(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
include('model.php');

if (($htmlString = @file_get_contents(URL)) === false) {
    continue;	
}
// Dữ liệu trang chủ
$html = str_get_html($htmlString);
// Lấy phần dữ liệu nhỏ trong menu
$menu = $html->find('ul.category-items', 0);
// Lấy dang sách danh mục trong menu
$level0 = $menu->find('li.level0');
$categories = array();
foreach ($level0 as $item0) {
	$href = $item0->find('a', 0)->href;
	$name = $item0->find('a span', 0)->plaintext;
	
	$level1 = $item0->find('li.level1');	
	$categories_level1 = array();
	foreach ($level1 as $item1) {
		$href1 = $item1->find('a', 0)->href;
		$name1 = $item1->find('a span', 0)->plaintext;
		
		$level2 = $item1->find('li.level2');	
		$categories_level2 = array();
		foreach ($level2 as $item2) {
			$href2 = $item2->find('a', 0)->href;
			$name2 = $item2->find('a span', 0)->plaintext;
			$categories_level2[] = array(
				'href'	=> $href2,
				'name' 	=> $name2
			);
		}
		$categories_level1[] = array(
			'href'	=> $href1,
			'name' 	=> $name1,
			'level2'=> $categories_level2
		);
	}
	
	$categories[] = array(
		'href'		=> $href,
		'name' 		=> $name,
		'level1'	=> $categories_level1
	);
}
/*
$i = 1;
foreach ($categories as $item0) {
	$parent_id0 = addCategory($item0['name'], $item0['href'], 0, $i);
	$i++;
	echo $item0['href'] . ': <b>' .$item0['name'] . '</b><br>';
	$j = 1;
	foreach ($item0['level1'] as $item1) {
		$parent_id1 = addCategory($item1['name'], $item1['href'], $parent_id0, $j);
		$j++;
		echo '----------' . $item1['href'] . ': <b>' .$item1['name'] . '</b><br>';	
		$k = 1;	
		foreach ($item1['level2'] as $item2) {
			$parent_id2 = addCategory($item2['name'], $item2['href'], $parent_id1, $k);
			$k++;
			echo '-----------------------' . $item2['href'] . ': <b>' .$item2['name'] . '</b><br>';
		}
	}
}
*/
?>